package cn.edu.dgut.css.sai.restfulsvc;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.math.BigDecimal;
import java.time.LocalDate;
import java.util.ArrayList;
import java.util.List;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.Resource;
import org.springframework.core.io.ResourceLoader;
import cn.edu.dgut.css.sai.restfulsvc.entity.Account;
import cn.edu.dgut.css.sai.restfulsvc.entity.Customer;
import cn.edu.dgut.css.sai.restfulsvc.repository.AccountRepository;
import cn.edu.dgut.css.sai.restfulsvc.repository.CustomerRepository;


@SpringBootApplication
public class RESTfulSvcApplication {

    public static void main(String[] args) {
        SpringApplication.run(RESTfulSvcApplication.class, args);
    }

}

@SuppressWarnings("unused")
@Configuration
class initDataSource {
    @Bean
    public CommandLineRunner commandLineRunner(AccountRepository accountRepository,CustomerRepository customerRepository, ResourceLoader resourceLoader) {
        return args -> {
            Resource resource1 = resourceLoader.getResource("classpath:MOCK_DATA.csv");
            Resource resource2 = resourceLoader.getResource("classpath:MOCK_DATA2.csv");
            List<Customer> users = parseCSVFileForCustom(resource1);
            List<Account> accounts = parseCSVFileForAccount(resource2);
            customerRepository.saveAll(users);
            accountRepository.saveAll(accounts);
        };
    }

    private List<Customer> parseCSVFileForCustom(final Resource Resource) throws Exception {
        final List<Customer> users = new ArrayList<>();
        try {
            try (final BufferedReader br = new BufferedReader(new InputStreamReader(Resource.getInputStream()))) {
                br.lines().forEach(line -> {
                    final String[] data = line.split(",");
                    final Customer user = new Customer();
                    user.setCustomerName(data[1]);
                    user.setDateofBirth(LocalDate.parse(data[2]));// 字符串 转 Localdate
                    user.setPhoneNumber(data[3]);
                    users.add(user);
                });
                return users;
            }
        } catch (final IOException e) {
            throw new Exception("转换 CSV file 失败{}", e);
        }
    }
    
    private List<Account> parseCSVFileForAccount(final Resource Resource) throws Exception {
        final List<Account> accounts = new ArrayList<>();
        try {
            try (final BufferedReader br = new BufferedReader(new InputStreamReader(Resource.getInputStream()))) {
                br.lines().forEach(line -> {
                	int i=0;
                    final String[] data = line.split(",");
                    final Account account = new Account();
                    account.setAccountName(data[1]);
                    account.setBalance(new BigDecimal("2.03"));
                    account.setOpeningDate(LocalDate.now());// 字符串 转 Localdate
                    account.setCustomer(i++);// 字符串 转 Localdate
                    accounts.add(account);
                });
                return accounts;
            }
        } catch (final IOException e) {
            throw new Exception("转换 CSV file 失败{}", e);
        }
    }
}
